/**
 * util.js
 *
 * depend on init.jsp, jquery, bootstrap, bootbox
 */
(function($, window, document,undefined) {
	var ModalContainer = [];
    //定义Modal的构造函数
    var Modal = function(opt) {
        this.options = opt
    }
    //定义Modal的方法
    Modal.prototype = {
		showLoading: function() {
			var _options = $.extend({closeButton: false, message: 'Loading...'}, this.options);
			_options.message = '<div class="text-center"><img src="'+Main.contextPath+'/static/images/loading.gif" style="margin-right: 10px;">'+_options.message+'</div>';
            var _modal = bootbox.dialog(_options);
            ModalContainer.push(_modal);
			return _modal;
        },
        hideLoading: function() {
        	var _modal = null;
        	while((_modal = ModalContainer.pop())){
        		_modal.modal('hide');
        	}
        },
        hideAll: function() {
        	bootbox.hideAll();
        },
        alert: function(opt, callback) {
        	if(callback){
        		bootbox.alert(opt, callback);
        	} else {
        		bootbox.alert(opt);
        	}
        }
    }
    
    //在插件中使用Modal对象
    $.extend({
    	showLoading : function(options) {
    		if(options){
    			if(typeof options == 'string'){
    				options = {message : options};
    			}
    		}
            //创建Modal的实体
            var modal = new Modal(options);
            //调用其方法
            return modal.showLoading();
        },
        hideLoading : function() {
            var modal = new Modal();
            return modal.hideLoading();
        },
        hideAllModals : function() {
            var modal = new Modal();
            return modal.hideAll();
        },
        alert : function(){
        	var argn = arguments.length;
        	var modal = new Modal();
        	if(argn == 1 && typeof arguments[0] == 'object'){
        		modal.alert(arguments[0]);
        	} else if (argn == 1 && typeof arguments[0] == 'string'){
        		modal.alert(arguments[0]);
        	} else if (argn == 2 && typeof arguments[0] == 'string' && $.isFunction(arguments[1])){
        		modal.alert(arguments[0], arguments[1]);
        	}
        }
    });
    
})(jQuery, window, document);